interface ShiosCacheQueryOptions {
  ignoreMethod?: boolean;
  ignoreSearch?: boolean;
  ignoreVary?: boolean;
}
class ShiosCache {
  private caches = new Map<string, ShiosResponse<any>>();
  async add(request: string): Promise<void> {
    request = request.toLowerCase();
  }
  async addAll(requests: RequestInfo[]): Promise<void> {}
  async delete(
    request: string,
    options?: ShiosCacheQueryOptions
  ): Promise<boolean> {
    request = request.toLowerCase();

    if (options?.ignoreSearch) {
      for (const i of this.caches) {
        if (i[0].split('?')[0] == request) {
          this.caches.delete(i[0]);
        }
      }
    }
    return this.caches.delete(request);
  }
  async keys(
    request?: string,
    options?: CacheQueryOptions
  ): Promise<ReadonlyArray<string>> {
    const keys: readonly string[] = [...this.caches.keys()];
    return keys;
  }
  async match<T>(
    request: string,
    options?: CacheQueryOptions
  ): Promise<ShiosResponse<T> | undefined> {
    request = request.toLowerCase();
    return this.caches.get(request);
  }

  async put<T>(request: string, response: ShiosResponse<T>): Promise<void> {
    request = request.toLowerCase();
    this.caches.set(request, response);
  }
}

export class ShiosCacheStorage {
  private caches = new Map<string, ShiosCache>();
  async delete(cacheName: string): Promise<boolean> {
    cacheName = cacheName.toLowerCase();
    return this.caches.delete(cacheName);
  }
  async has(cacheName: string): Promise<boolean> {
    cacheName = cacheName.toLowerCase();
    return this.caches.has(cacheName);
  }
  async keys(): Promise<string[]> {
    return [...this.caches.keys()];
  }
  async match<T>(request: string): Promise<ShiosResponse<T> | undefined> {
    request = request.toLowerCase();
    for (const cache of this.caches) {
      let response = await cache[1].match<T>(request);
      if (response) {
        return response;
      }
    }
    return undefined;
  }
  async open(cacheName: string): Promise<ShiosCache> {
    cacheName = cacheName.toLowerCase();
    let result = this.caches.get(cacheName);
    if (!result) {
      result = new ShiosCache();
      this.caches.set(cacheName, result);
    }
    return result;
  }
}
// export const caches: CacheStorage | ShiosCacheStorage =
//   typeof window.caches != 'undefined' ? window.caches : new ShiosCacheStorage();
